Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jinja2 instrumentation #643

Merged
merged 29 commits into from
May 11, 2020

Conversation

majorgreys
Copy link
Contributor

Fixes #631

@majorgreys majorgreys requested a review from a team as a code owner May 4, 2020 04:38
@majorgreys majorgreys force-pushed the majorgreys/jinja2 branch 2 times, most recently from 4cac1a8 to 263f6aa Compare May 4, 2020 04:42
try:
return wrapped(*args, **kwargs)
finally:
span.set_attribute("component", "template")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no component any longer in the semantic conventions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a link for the background on that change? This has been unclear to me when looking over the code.

Copy link
Member

@Oberon00 Oberon00 May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was done in open-telemetry/opentelemetry-specification#447, see also open-telemetry/opentelemetry-specification#271. Basically, it was deemed redundant. Previously you could check for component == http to determine if it is an HTTP span, now you can still check for hasAttribute(http.method).

return wrapped(*args, **kwargs)
finally:
span.set_attribute("component", "template")
span.set_attribute("jinja2.template_name", template_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template engines are widely used across different languages. Have you considered bringing up a spec PR for semantic conventions for templating engines? I think we should aim for cross-language cross-framework consistency here. E.g. maybe templating.engine=jinja2, templating.template=FILENAME would be good candidates?

As long as we don't have such conventions, using jinja2 as prefix is however probably the most name-clash-safe way.

Also, please set these attributes already when creating the span, so that they are available to samplers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly agree about a spec PR and can start a conversation about that as a follow-up from this PR.

Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall.

Few comments about some details I found around.

ext/opentelemetry-ext-jinja2/README.rst Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/setup.cfg Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/setup.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/conftest.py Outdated Show resolved Hide resolved
Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, all the comments are simple nits.

I tested by using:

$ cat templates/base.html 
Message: {% block content %}{% endblock %}
$ cat templates/template.html 
{% extends 'base.html' %}
{% block content %}Hello {{name}}!{% endblock %}
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    SimpleExportSpanProcessor,
    ConsoleSpanExporter,
)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
  SimpleExportSpanProcessor(ConsoleSpanExporter())
)


from jinja2 import Environment, FileSystemLoader
from opentelemetry.ext.jinja2 import Jinja2Instrumentor

Jinja2Instrumentor().instrument()

env = Environment(loader=FileSystemLoader("templates"))
template = env.get_template('template.html')

I see how two spans are generated.

ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
majorgreys and others added 5 commits May 8, 2020 08:30
Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
@codeboten codeboten added the needs reviewers PRs with this label are ready for review and needs people to review to move forward. label May 8, 2020
Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM again. Thanks for handling all my comments.

Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple small comments, but otherwise LGTM!

ext/opentelemetry-ext-jinja2/setup.cfg Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/setup.cfg Outdated Show resolved Hide resolved
)


def _unwrap(obj, attr):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an aside, but unwrapping looks to be everywhere now. We might want to see what we can do to consolidate: #667

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, will comment on the issue and we can refactor the unwrapping code accordingly.

ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
ext/opentelemetry-ext-jinja2/tests/test_jinja2.py Outdated Show resolved Hide resolved
@toumorokoshi toumorokoshi merged commit 1140332 into open-telemetry:master May 11, 2020
@majorgreys majorgreys deleted the majorgreys/jinja2 branch May 11, 2020 18:25
owais pushed a commit to owais/opentelemetry-python that referenced this pull request May 22, 2020
Migrating the jinja2 plugin forked from ddtracepy.

Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io>
srikanthccv pushed a commit to srikanthccv/opentelemetry-python that referenced this pull request Nov 1, 2020
* feat(plugin-http): add/modify attributes

closes open-telemetry#373, open-telemetry#394

Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>

* fix: change remotePort to localPort

refactor: remove useless checks
test: add assertions

Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>

* test(plugin-https): sync with http plugin

Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>

Co-authored-by: Mayur Kale <mayurkale@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs reviewers PRs with this label are ready for review and needs people to review to move forward.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add instrumentation for jinja2
5 participants